package com.hzsk.tool.controller.toolanalysis;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.diboot.core.util.V;
import com.diboot.core.util.S;
import com.diboot.core.util.ContextHolder;
import com.diboot.core.vo.JsonResult;
import com.diboot.core.vo.LabelValue;
import com.diboot.core.vo.Pagination;
import com.diboot.core.controller.BaseCrudRestController;
import com.diboot.iam.annotation.OperationCons;
import com.diboot.iam.annotation.BindPermission;
import com.diboot.iam.annotation.Log;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.hzsk.tool.entity.toolanalysis.ToolInventoryReport;
import com.hzsk.tool.service.toolanalysis.ToolInventoryReportService;
import com.hzsk.tool.vo.toolanalysis.ToolInventoryReportDetailVO;
import com.hzsk.tool.vo.toolanalysis.ToolInventoryReportListVO;
import com.hzsk.tool.dto.toolanalysis.ToolInventoryReportDTO;

import java.io.Serializable;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 刀具库存报表 相关rest接口
 *
 * @author MyName
 * @version 1.0
 * @date 2024-11-05
 * Copyright © MyCorp
 */
@Slf4j
@RequestMapping("/tool-analysis/tool-inventory-report")
@BindPermission(name = "刀具库存报表")
@RestController
@CrossOrigin(origins = "*")
public class ToolInventoryReportController extends BaseCrudRestController<ToolInventoryReport> {

    @Autowired
    private ToolInventoryReportService toolInventoryReportService;

    /**
     * 查询资源对象的列表VO记录
     * <p>
     * url请求参数示例: ?fieldA=abc&pageSize=20&pageIndex=1&orderBy=id
     * </p>
     *
     * @return
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_LIST)
    @BindPermission(name = OperationCons.LABEL_LIST, code = OperationCons.CODE_READ)
    @GetMapping()
    public JsonResult<List<ToolInventoryReportListVO>> getListVOMapping(ToolInventoryReportDTO queryDto, Pagination pagination) throws Exception {
        return super.getViewObjectList(queryDto, pagination, ToolInventoryReportListVO.class);
    }

    /**
     * 根据id查询资源对象的详情VO
     *
     * @param id ID
     * @return
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_DETAIL)
    @BindPermission(name = OperationCons.LABEL_DETAIL, code = OperationCons.CODE_READ)
    @GetMapping("/{id}")
    public JsonResult<ToolInventoryReportDetailVO> getDetailVOMapping(@PathVariable("id") String id) throws Exception {
        ToolInventoryReportDetailVO detailVO = toolInventoryReportService.getViewObject(id, ToolInventoryReportDetailVO.class);
        return JsonResult.OK(detailVO);
    }


    /**
     * 创建资源对象数据
     *
     * @param toolInventoryReport
     * @return JsonResult
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_CREATE)
    @BindPermission(name = OperationCons.LABEL_CREATE, code = OperationCons.CODE_WRITE)
    @PostMapping()
    public JsonResult<Serializable> createModelMapping(@RequestBody ToolInventoryReport toolInventoryReport) throws Exception {
        ToolInventoryReport entity = toolInventoryReportService.lambdaQuery()
                .eq(ToolInventoryReport::getMaterialNumber, toolInventoryReport.getMaterialNumber()).eq(ToolInventoryReport::isDeleted, false).one();
        if (entity != null) {
            toolInventoryReport.setId(entity.getId());
        }
        toolInventoryReportService.createOrUpdateEntity(toolInventoryReport);
        return JsonResult.OK(toolInventoryReport.getId());
    }

    /**
     * 根据id更新资源对象
     *
     * @param toolInventoryReport
     * @return JsonResult
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_UPDATE)
    @BindPermission(name = OperationCons.LABEL_UPDATE, code = OperationCons.CODE_WRITE)
    @PutMapping("/{id}")
    public JsonResult<?> updateModelMapping(@PathVariable("id") String id, @RequestBody ToolInventoryReport toolInventoryReport) throws Exception {
        toolInventoryReportService.updateEntity(toolInventoryReport);
        return JsonResult.OK();
    }

    /**
     * 根据id删除资源对象
     *
     * @param mrlCode
     * @return
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_DELETE)
    @BindPermission(name = OperationCons.LABEL_DELETE, code = OperationCons.CODE_WRITE)
    @DeleteMapping("/{mrlCode}")
    public JsonResult<?> deleteModelMapping(@PathVariable("mrlCode") String mrlCode) throws Exception {
        boolean b = toolInventoryReportService.deleteEntities(new QueryWrapper<ToolInventoryReport>().lambda().eq(ToolInventoryReport::getMaterialNumber, mrlCode));
        log.debug("删除数据 刀具库存报表:{} {}", mrlCode, b ? "成功" : "失败");
        return JsonResult.OK();
    }

    /**
     * 根据ids集合删除资源对象
     *
     * @param ids 物料号
     * @return
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_BATCH_DELETE)
    @BindPermission(name = OperationCons.LABEL_DELETE, code = OperationCons.CODE_WRITE)
    @PostMapping("/batch-delete")
    public JsonResult<?> deleteEntityMapping(@RequestBody List<String> ids) throws Exception {
        if (ids == null || ids.isEmpty()) {
            return JsonResult.OK();
        }
        boolean success = toolInventoryReportService.deleteEntities(new QueryWrapper<ToolInventoryReport>().lambda().in(ToolInventoryReport::getMaterialNumber, ids));
        log.debug("批量删除数据 刀具库存报表:{} {}", ids, success ? "成功" : "失败");
        return JsonResult.OK();
    }

    /**
     * 批量创建资源对象数据
     *
     * @param toolInventoryReport
     * @return JsonResult
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_CREATE)
    @BindPermission(name = OperationCons.LABEL_CREATE, code = OperationCons.CODE_WRITE)
    @PostMapping("/batch-add")
    public JsonResult<Serializable> createModelMappingBatch(@RequestBody List<ToolInventoryReport> toolInventoryReport) throws Exception {
        List<ToolInventoryReport> list = toolInventoryReportService.lambdaQuery().in(ToolInventoryReport::getMaterialNumber, toolInventoryReport.stream().map(ToolInventoryReport::getMaterialNumber).collect(Collectors.toList())).list();
        for (ToolInventoryReport inventoryReport : toolInventoryReport) {
            inventoryReport.setId(list.stream().filter(item -> item.getMaterialNumber().equals(inventoryReport.getMaterialNumber())).map(ToolInventoryReport::getId).findFirst().orElse(null));
        }
        toolInventoryReportService.createOrUpdateEntities(toolInventoryReport);
        return JsonResult.OK();
    }
}